package com.quanquan.store.controller;

import DTO.PageDTO;
import DTO.coupon.CouponInfoDTO;
import com.quanquan.store.dto.*;
import com.quanquan.store.entity.Admin;
import com.quanquan.store.request.*;
import com.quanquan.store.service.CouponService;
import com.quanquan.store.service.HistoryService;
import com.quanquan.store.service.OrderService;
import com.quanquan.store.service.SendCouponRecordService;
import enums.ResCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import utils.StringUtil;
import utils.StringUtils;
import web.response.ResultBody;

import java.util.List;
import java.util.Map;

/**
 * @ClassName:CouponController
 * @Description:TODO
 * @Author:xiongjunbao
 * @Date:2019/3/18 14:59
 * @Version 1.0
 */
@RestController
@RequestMapping("/coupon")
public class CouponController {

    public static final String OVER_COUPON_COUNT = "超出卡券当天兑换数量限制";
    public static final String NO_AUTH = "无权限核销此券";
    private static final String NO_GOODS = "用户没有对应商品";
    private static final String NOT_ENOUGH = "商品数量不足";
    private static final String USER_NO_CENT = "用户积分不足";
    private static final String CANCAL_COUPON = "核销成功";
    private static final String UP_SUCCESS = "上架成功";
    private static final String DOWN_SUCCESS = "下架成功";
    private static final String NOT_TRUE = "账号或者密码不正确";
    private static final String SQUARE_COUNT = "已达到当天商城限制兑换数量";
    private static final String COUPON_COUNT = "已达到当前券当天限制兑换数量";
    public static final String OVER_SQUARE_COUNT = "超出商城当天兑换数量限制";
    public static final String OVER_DATE = "该券已过期";
    public static final String ERROR_TYPE = "券类型错误";
    public static final String ALREADY = "券已核销";
    public static final String NOT_CATCH_TIME = "未到使用时间";
    public static final String OVER_BUY_TIME = "超出有效期内兑换数量限制";
    public static final String NO_IN_TIME = "未到购买时间";
    public static final String SEND_SUCCESS = "发券成功";
    private final CouponService couponService;
    private final OrderService orderService;
    private final HistoryService historyService;
    private final SendCouponRecordService sendCouponRecordService;
    @Autowired
    public CouponController(CouponService couponService, OrderService orderService, HistoryService historyService, SendCouponRecordService sendCouponRecordService) {
        this.couponService = couponService;
        this.orderService = orderService;
        this.historyService = historyService;
        this.sendCouponRecordService = sendCouponRecordService;
    }

    /**
     * 添加优惠券（后台）
     *
     * @param request
     * @return
     */
    @PostMapping("/add_coupon")
    @ResponseBody
    public ResultBody addCoupon(@RequestBody CouponRequest request) {
        if (StringUtils.isEmpty(request.getName())) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), "商品信息不完整");
        }
        boolean flag = couponService.saveCoupon(request);
        if (flag) {
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS", "创建券成功");
        } else {
            return new ResultBody(ResCodeEnum.FAIL.getCode(), "FAIL", "创建券失败");
        }
    }

    /**
     * 修改券
     *
     * @param request
     * @return
     */
    @PostMapping("/update_coupon")
    public ResultBody updateCoupon(@RequestBody CouponRequest request) {
        if (StringUtils.isEmpty(request.getId())) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), "商品id不可为空");
        }
        boolean flag = couponService.updateCoupon1(request);
        if (flag) {
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS", "修改券信息成功");
        } else {
            return new ResultBody(ResCodeEnum.FAIL.getCode(), "FAIL", "卡券不存在或券未下架");
        }
    }

    /**
     * 查询优惠券列表（前端）
     *
     * @param request
     * @return
     */
    @PostMapping("/query_list")
    public ResultBody queryList(@RequestBody CouponRequest request) {
        List<CouponDTO> couponDTOList = couponService.queryList1(request);
        if (couponDTOList.size() <= 0) {
            return new ResultBody(ResCodeEnum.NO_DATA.getCode(), "没有对应的商品");
        }
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS", couponDTOList);
    }


    /**
     * 多条件查询
     *
     * @param request
     * @return
     */
    @PostMapping("query_select")
    public ResultBody queryByCondition(@RequestBody CouponRequest request) {
        PageDTO<CouponDTO> couponPageDTO;

        couponPageDTO = couponService.queryByCondition(request);
        if (null != couponPageDTO && couponPageDTO.getList().size() > 0) {
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg(), couponPageDTO);
        }
        return new ResultBody(ResCodeEnum.NO_DATA.getCode(), ResCodeEnum.NO_DATA.getMsg());

    }

    /**
     * 查询优惠券列表/热门精品（statusHot=1）(前端)
     *
     * @param request
     * @return
     */
    @PostMapping("/query_by_id")
    public ResultBody queryById(@RequestBody CouponRequest request) {
        CouponDTO couponDTO = couponService.queryById1(request.getId(), request.getQueryStatus(), request.getUserId());
        if (couponDTO == null) {
            return new ResultBody(ResCodeEnum.NO_DATA.getCode(), "没有对应的商品");
        }
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS", couponDTO);
    }

    @PostMapping("/query_details_by_id")
    public ResultBody findDetailsById(@RequestBody CouponRequest request) {
        String couponId = request.getId();
        CouponDTO couponDTO = couponService.queryDetailsById(couponId);
        if (couponDTO == null) {
            return new ResultBody(ResCodeEnum.NO_DATA.getCode(), "没有对应的商品");
        }
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS", couponDTO);
    }

    /**
     * 模糊查询商品
     *
     * @param request
     * @return
     */
    @PostMapping("query_list_by_coupon_name")
    public ResultBody queryListByCouponName(@RequestBody CouponRequest request) {
        List<CouponDTO> couponDTOList = couponService.queryListByCouponName1(request);
        if (couponDTOList.size() <= 0) {
            return new ResultBody(ResCodeEnum.NO_DATA.getCode(), "暂无匹配商品");
        }
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS", couponDTOList);
    }

    /**
     * 根据优惠券类型查询列表
     *
     * @param request
     * @return
     */
    @PostMapping("query_list_by_type")
    public ResultBody queryListByType(@RequestBody CouponRequest request) {
        List<CouponDTO> couponDTOList = couponService.queryListByType(request);
        if (couponDTOList.size() <= 0) {
            return new ResultBody(ResCodeEnum.NO_DATA.getCode(), "暂无匹配商品");
        }
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS", couponDTOList);
    }

    /**
     * 根据优惠券支付类型查询列表
     *
     * @param request
     * @return
     */
    @PostMapping("query_list_by_value_type")
    public ResultBody queryListByValueType(@RequestBody CouponRequest request) {
        List<CouponDTO> couponDTOList = couponService.queryListByValueType(request);
        if (couponDTOList.size() <= 0) {
            return new ResultBody(ResCodeEnum.NO_DATA.getCode(), "暂无匹配商品");
        }
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS", couponDTOList);
    }


    /**
     * 根据优惠券支付类型和券类型联合查询列表
     *
     * @param request
     */
    @PostMapping("query_list_by_value_type_and_type")
    public ResultBody queryListByValueTypeAndType(@RequestBody CouponRequest request) {
        List<CouponDTO> couponDTOList = couponService.queryListByValueTypeAndType1(request);
        if (couponDTOList.size() <= 0) {
            return new ResultBody(ResCodeEnum.NO_DATA.getCode(), "暂无匹配商品");
        }
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS", couponDTOList);
    }

    /**
     * 生成订单
     *
     * @param request
     * @return
     */
    @PostMapping("/create_order")
    public ResultBody createOrder(@RequestBody OrderRequest request) {
        /*if (StringUtils.isEmpty(request.getShopMallId())) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), "商品id不可为空");
        }*/
        if (StringUtils.isEmpty(request.getUserId())) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), "用户id不可为空");
        }
        if (request.getCount() <= 0) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), "购买数量不能小于等于0");
        }
        OrderDTO orderDTO = orderService.saveOrder2(request);
        if (orderDTO.getMsg()!=null){
            return new ResultBody(ResCodeEnum.CUSTOM.getCode(), orderDTO.getMsg());
        }
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS", orderDTO);
    }

    /**
     * 查看我的优惠券
     *
     * @param request
     * @return
     */
    @PostMapping("/query_coupon_code_by_user_id")
    public ResultBody queryCouponCodeByUserId(@RequestBody CouponCodeRequest request) {
        List<Object> objectList = couponService.queryCodeListByUserId(request);
        if (objectList.size() <= 0) {
            return new ResultBody(ResCodeEnum.NO_DATA.getCode(), "暂无内容");
        }
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS", objectList);
    }

    /**
     * 查看我的停车券
     *
     * @param request
     * @return
     */
    @PostMapping("/query_parking_coupon_by_user_id")
    public ResultBody queryParkingCouponByUserId(@RequestBody CouponCodeRequest request) {
        List<Object> objectList = couponService.queryParkingCouponByUserId(request);
        if (objectList.size() <= 0) {
            return new ResultBody(ResCodeEnum.NO_DATA.getCode(), "暂无内容");
        }
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS", objectList);
    }

    /**
     * 查看我的优惠券详情
     *
     * @param request
     * @return
     */
    @PostMapping("/query_code_by_id")
    public ResultBody queryCodeById(@RequestBody CouponCodeRequest request) {
        if (StringUtils.isEmpty(request.getId())) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), "未指明id");
        }
        Map<String, Object> map = couponService.queryCodeByIdAndUserId1(request);
        if (map.isEmpty()) {
            return new ResultBody(ResCodeEnum.NO_DATA.getCode(), "暂无内容");
        }
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS", map);
    }

    /**
     * 查看积分兑换商品订单
     *
     * @param request
     * @return
     */
    @PostMapping("/query_list_by_user_id")
    public ResultBody queryListByUserId(@RequestBody OrderRequest request) {
        List<Object> objectList = orderService.queryListByUserId(request);
        if (objectList.size() <= 0) {
            return new ResultBody(ResCodeEnum.NO_DATA.getCode(), "暂无内容");
        }
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS", objectList);
    }

    /**
     * 积分商城全年消费总积分
     *
     * @param request
     * @return
     */
    @PostMapping("/query_by_user_id_year")
    public ResultBody queryByUserIdYear(@RequestBody OrderRequest request) {
        Map<String, Object> map = orderService.queryByUserIdYear(request);
        if (map.isEmpty()) {
            return new ResultBody(ResCodeEnum.NO_DATA.getCode(), "暂无内容");
        }
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS", map);
    }

    /**
     * 简易登录
     *
     * @param request
     * @return
     */
    @PostMapping("/login")
    public ResultBody login(@RequestBody AdminRequest request) {
        if (StringUtils.isEmpty(request.getAccount())) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), "账号为空");
        }
        if (StringUtils.isEmpty(request.getPassword())) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), "密码为空");
        }
        Admin admin = couponService.login(request);
        if (admin == null) {
            return new ResultBody(ResCodeEnum.FAIL.getCode(), "登陆失败");
        } else {
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "登陆成功", admin);
        }
    }

    /**
     * 确认核销用户
     *
     * @param request
     * @return
     */
    @PostMapping("/check")
    public ResultBody checkPassword(@RequestBody AdminRequest request) {
        if (StringUtils.isEmpty(request.getAccount())) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), "账号为空");
        }
        if (StringUtils.isEmpty(request.getPassword())) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), "密码为空");
        }
        boolean flag = couponService.checkPassword(request);
        if (flag) {
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS");
        } else {
            return new ResultBody(ResCodeEnum.FAIL.getCode(), "原密码错误");
        }
    }

    /**
     * 修改核销用户密码
     *
     * @param request
     * @return
     */
    @PostMapping("/edit")
    public ResultBody edit(@RequestBody AdminRequest request) {
        if (StringUtils.isEmpty(request.getAccount())) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), "账号为空");
        }
        if (StringUtils.isEmpty(request.getPassword())) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), "密码为空");
        }
        if (StringUtils.isEmpty(request.getTruePassword())) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), "确认密码为空");
        }
        if (!request.getTruePassword().equals(request.getPassword())) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), "两次密码不一致");
        }
        boolean flag = couponService.editPassword(request);
        if (flag) {
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "密码修改成功");
        } else {
            return new ResultBody(ResCodeEnum.FAIL.getCode(), "密码修改失败");
        }
    }

    /**
     * 核销优惠券
     *
     * @param request
     * @return
     */
    @PostMapping("/cancel_coupon_code")
    public ResultBody cancelCouponCode(@RequestBody CouponCodeRequest request) {
        if (StringUtils.isEmpty(request.getCouponCode())) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), "券码不可为空");
        }
        String msg = couponService.cancelCouponCode(request);
        if (msg.equals(NO_GOODS)) {
            return new ResultBody(ResCodeEnum.NO_DATA.getCode(), "该用户没用该商品可使用");
        }
        if (msg.equals(ERROR_TYPE)) {
            return new ResultBody(ResCodeEnum.CUSTOM.getCode(), "停车券不可核销");
        }
        if (msg.equals(OVER_DATE)) {
            return new ResultBody(ResCodeEnum.CUSTOM.getCode(), "该券已过期");
        }
        if (msg.equals(NOT_CATCH_TIME)) {
            return new ResultBody(ResCodeEnum.CUSTOM.getCode(), "未到使用时间");
        }
        if (msg.equals(NO_AUTH)) {
            return new ResultBody(ResCodeEnum.CUSTOM.getCode(), "无权限核销此券");
        }
        if (msg.equals(ALREADY)) {
            return new ResultBody(ResCodeEnum.CUSTOM.getCode(), "该券已核销");
        }
        if (msg.equals(CANCAL_COUPON)) {
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "核销成功");
        } else {
            return new ResultBody(ResCodeEnum.FAIL.getCode(), "核销失败");
        }
    }

    /**
     * 通过券码看券详情
     *
     * @param request
     * @return
     */
    @PostMapping("/query_by_coupon_code")
    public ResultBody queryByCouponCode(@RequestBody CouponCodeRequest request) {
        if (StringUtils.isEmpty(request.getCouponCode())) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), "券码不可为空");
        }
        Map<String, Object> map = couponService.queryByCouponCode(request.getCouponCode());
        System.out.println(map);
        if (map == null) {
            return new ResultBody(ResCodeEnum.NO_DATA.getCode(), "券码错误");
        }
        if (map.get("type").equals(1)) {
            return new ResultBody(ResCodeEnum.CUSTOM.getCode(), "券已被核销");
        }
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS", map);
    }

    /**
     * 优惠券核销记录
     *
     * @param request
     * @return
     */
    @PostMapping("/cancel_record")
    public ResultBody cancelRecord(@RequestBody CouponCodeRequest request) {
        List<Object> objectList = couponService.cancelRecord(request);
        if (objectList.size() <= 0) {
            return new ResultBody(ResCodeEnum.NO_DATA.getCode(), "暂无数据");
        }
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS", objectList);
    }

    @PostMapping("/query/record_select")
    public ResultBody cancelRecordByCondition(@RequestBody CouponCodeRequest request) {
        PageDTO<CouponCodeDTO> couponCodePageDTO = couponService.cancelRecordByCondition(request);
        if (null == couponCodePageDTO) {
            return new ResultBody(ResCodeEnum.FAIL.getCode(), ResCodeEnum.FAIL.getMsg());
        }
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg(), couponCodePageDTO);
    }

    /**
     * 上架、下架优惠券
     *
     * @param request
     * @return
     */
    @PostMapping("/change_coupon_status")
    public ResultBody changeCouponStatus(@RequestBody CouponRequest request) {
        if (request.getStatus() == null) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), "券状态不可为空");
        }
        if (StringUtils.isEmpty(request.getId())) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), "券id不可为空");
        }
        String msg = couponService.changeCouponStatus(request);
        if (msg.equals(UP_SUCCESS)) {
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS", msg);
        }
        if (msg.equals(DOWN_SUCCESS)) {
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS", msg);
        } else {
            return new ResultBody(ResCodeEnum.FAIL.getCode(), "FAIL");
        }
    }


    /**
     * 查看历史记录
     *
     * @param request
     * @return
     */
    @PostMapping("/query_histoty")
    public ResultBody queryHistoty(@RequestBody CouponRequest request) {
        if (StringUtils.isEmpty(request.getUserId())) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), "未指明用户id");
        }
        List<Object> objectList = couponService.queryByUserId(request);
        if (objectList.size() <= 0) {
            return new ResultBody(ResCodeEnum.NO_DATA.getCode(), "无记录");
        }
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS", objectList);
    }

    /**
     * 删除历史搜索记录
     *
     * @param request
     * @return
     */
    @PostMapping("/remove_histoty")
    public ResultBody removeHistoty(@RequestBody CouponRequest request) {
        if (StringUtils.isEmpty(request.getUserId())) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), "未指明用户id");
        }
        historyService.removeCouponHistory(request.getUserId());
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "删除搜索历史成功");
    }

    /**
     * 停车券使用，账单减免计费接口(点击优惠券，账单减免计费接口)
     *
     * @param request
     * @return
     */
    @PostMapping("/use_parking_coupon")
    public ResultBody useParkingCoupon(@RequestBody CouponCodeRequest request) {
        if (StringUtils.isEmpty(request.getCouponCode())) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), "券码不可为空");
        }
        Map<String, Object> map = couponService.useParkingCoupon(request);
        if (map != null && map.containsKey("msg") && map.get("msg").equals("SUCCESS")) {
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg(), map);
        } else {
            return new ResultBody(ResCodeEnum.CUSTOM.getCode(), (String) map.get("msg"));
        }
    }

    /**
     * 停车券使用
     *
     * @param request
     * @return
     */
    @PostMapping("/cancel_parking_coupon")
    public ResultBody cancelParkingCoupon(@RequestBody CouponCodeRequest request) {
        if (StringUtils.isEmpty(request.getCouponCode())) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), "券码不可为空");
        }
        String msg = couponService.cancelParkingCoupon(request);
        if (msg.equals("核销成功")) {
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS");
        }
        return new ResultBody(ResCodeEnum.FAIL.getCode(), msg);
    }

    /**
     * 手动发券
     *
     * @param request
     * @return
     */
    @PostMapping("/send_coupon")
    public ResultBody sendCoupon(@RequestBody CouponRequest request) {
        if (StringUtils.isEmpty(request.getUserId()) && request.getUserIdList() == null) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), "用户id不可为空");
        }
        String msg = couponService.sendCoupon(request);
        if (NO_GOODS.equals(msg)) {
            return new ResultBody(ResCodeEnum.CUSTOM.getCode(), "该商品未上架");
        }
        if (NOT_ENOUGH.equals(msg)) {
            return new ResultBody(ResCodeEnum.CUSTOM.getCode(), "商品数量不足");
        }
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS", msg);
    }

    /**
     * 停车券使用记录
     *
     * @return
     */
    @PostMapping("/parking_record")
    public ResultBody parkingRecord(@RequestBody CouponCodeRequest request) {
        PageDTO<CouponCodeDTO> couponCodeDTOPageDTO = couponService.parkingRecord(request);
        if (null == couponCodeDTOPageDTO) {
            return new ResultBody(ResCodeEnum.FAIL.getCode(), ResCodeEnum.FAIL.getMsg());
        }
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS", couponCodeDTOPageDTO);
    }


    /**
     * 后台用详情接口
     *
     * @param request
     * @return
     */
    @PostMapping("/query_by_id_manage")
    public ResultBody queryByIdManage(@RequestBody CouponRequest request) {
        CouponDTO couponDTO = couponService.queryByIdManage(request.getId());
        if (null == couponDTO) {
            return new ResultBody(ResCodeEnum.FAIL.getCode(), ResCodeEnum.FAIL.getMsg());
        }
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS", couponDTO);
    }

    /**
     * 停车券使用记录
     *
     * @return
     */
    @PostMapping("/send_record")
    public ResultBody senderRecord(@RequestBody CouponCodeRequest request) {
        PageDTO<CouponCodeDTO> couponCodeDTOPageDTO = couponService.sendRecord(request);
        if (null == couponCodeDTOPageDTO) {
            return new ResultBody(ResCodeEnum.FAIL.getCode(), ResCodeEnum.FAIL.getMsg());
        }
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS", couponCodeDTOPageDTO);
    }

    /**
     * 优惠券获取总数
     *
     * @return
     */
    @PostMapping("/coupon_count")
    public ResultBody couponCount() {
        Map<String, Integer> couponCount = couponService.couponCount();
        if (couponCount.isEmpty()) {
            return new ResultBody(ResCodeEnum.FAIL.getCode(), "FAIL");
        }
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS", couponCount);
    }

    /**
     * 未过期停车券列表，后台用
     *
     * @return
     */
    @PostMapping("/query_parking_coupon_list")
    public ResultBody queryParkingCouponList(@RequestBody CouponRequest request) {
        List<CouponDTO> couponDTOList = couponService.findAlls(3);
        if (couponDTOList.size() <= 0) {
            return new ResultBody(ResCodeEnum.FAIL.getCode(), "FAIL");
        }
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS", couponDTOList);
    }

    /**
     * 未过期优惠券列表，后台用
     *
     * @return
     */
    @PostMapping("/query_coupon_list")
    public ResultBody queryCouponList(@RequestBody CouponRequest request) {
        List<CouponDTO> couponDTOList = couponService.findAlls(1);
        if (couponDTOList.size() <= 0) {
            return new ResultBody(ResCodeEnum.FAIL.getCode(), "FAIL");
        }
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS", couponDTOList);
    }

    /**
     * 未过期优惠券列表，后台用
     *
     * @return
     */
    @PostMapping("/query_coupon_new_list")
    public ResultBody queryCouponNewList(@RequestBody CouponRequest request) {
        List<CouponDTO> couponDTOList = couponService.findAllss(1);
        if (couponDTOList.size() <= 0) {
            return new ResultBody(ResCodeEnum.FAIL.getCode(), "FAIL");
        }
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS", couponDTOList);
    }

    /**
     * 未过期优惠券列表，后台用
     *
     * @return
     */
    @PostMapping("/recharge_to_store")
    public ResultBody rechargeToStore(@RequestBody CouponRequest request) {
        if (StringUtil.isEmpty(request.getStoreId())) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), "请选择充值商户");
        }
        if (request.getCount() == null || request.getCount() == 0) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), "请填写充值数量");
        }
        String msg = couponService.sendCouponToStore(request);
        if (SEND_SUCCESS.equals(msg)) {
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "发券成功");
        } else {
            return new ResultBody(ResCodeEnum.FAIL.getCode(), "发券失败");
        }
    }

    /**
     * 未过期优惠券列表，后台用
     *
     * @return
     */
    @PostMapping("/store_send_coupon_to_user")
    public ResultBody storeSendCouponToUser(@RequestBody CouponRequest request) {
        if (StringUtil.isEmpty(request.getStoreId())) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), "请选择充值商户");
        }
        if (request.getCount() == null || request.getCount() == 0) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), "请填写充值数量");
        }
        String msg = couponService.storeSendCouponToUser(request);
        if (SEND_SUCCESS.equals(msg)) {
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "发券成功");
        } else {
            return new ResultBody(ResCodeEnum.FAIL.getCode(), "发券失败");
        }
    }

    /**
     * 通过idList查找券列表
     * @param idDTO
     * @return
     */
    @PostMapping("/query_by_id_list")
    @ResponseBody
    public CouponInfoDTO queryByIdList(@RequestBody IdDTO idDTO){

        CouponInfoDTO couponInfoDTO = new CouponInfoDTO();

        couponInfoDTO.setCouponDTOS(couponService.queryByIdList(idDTO.getForeignKeyIds()));
        return couponInfoDTO;
    }

    /**
     * 发券记录
     *
     * @return
     */
    @PostMapping("/send_coupon_record")
    public ResultBody sendCouponRecord(@RequestBody SendCouponRecordRequest request) {
        PageDTO<SendCouponRecordDTO> pageDTO = sendCouponRecordService.queryByCondition(request);
        if (pageDTO==null||pageDTO.getList().size()<=0) {
            return new ResultBody(ResCodeEnum.NO_DATA.getCode(), "暂无数据");
        } else {
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS",pageDTO);
        }
    }



}
